
import 'package:dio/dio.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBHelper/YBCommonToast/YBCommonToastPage.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBJsonToModel/YBBaseModel.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBNetWorkModule/YBAPi/YBAPi.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBNetWorkModule/YBAPi/YBLogUtil.dart';
import 'YBDioUtils.dart';

typedef Success<T> = Function(T data);
typedef Fail = Function(int code);

class YBHttpUtils {

  // static void login<T>(parameters,{Success success, Fail fail}){
  //   post(YBApi.loginIn,parameters,success:success,fail:fail);
  // }



  /********************************* 分割线 ********************************/

  //get 请求
  static void get<T>(String url, parameters,{Success success, Fail fail}){
    _request(Method.GET,url,parameters, success:success,fail:fail);
  }


  //post 请求
  static void post<T>(String url, paramters,{Success success, Fail fail}){
    _request(Method.POST, url,paramters, success: success,fail: fail);
  }

  //_request 请求
  static void _request<T>(
      Method method, String url, parameters,{Success success,Fail fail}){
    /// restful 请求处理
    /// /base/search/hist/:user_id        user_id=27
    /// 最终生成 url 为     /base/search/hist/27
    parameters.forEach((key, value){
      if (url.indexOf(key) != -1) {
        url = url.replaceAll(':$key', value.toString());
      }
    });

    //    //参数处理
    YBLogUtils.d("--------- parameters ---------");
    YBLogUtils.d("$parameters");
    YBDioUtils.request(method, url, parameters,success: (result){
      //      LogUtils.d("--------- response ---------");
      YBLogUtils.d('$result');
      //      LogUtils.print_(result);
      if (result['code'] == 0) {
        if (success != null) {
          success(result);
        } else {
          YBToast.showToastInfo(result['msg']);
        }
      }else if (result['code'] == 10000) {
        if (success != null) {
          success(result);
        } else {
          YBToast.showToastInfo(result['msg']);
        }
      }else{
        if (success != null) {
          success(result);
        } else {
          YBToast.showToastInfo(result['msg']);
        }
      }

    },fail:(code, msg) {
      YBToast.showToastInfo(msg);
      if (fail != null) {
        fail(code);
      }
    });
  }

  //get 请求
  static void getRequest<T>(String url, parameters,{Success success, Fail fail}){
    _requestGet(Method.GET,url,parameters, success:success,fail:fail);
  }

  //响应接口没有处理的get
  static void getResponseRequest<T>(String url, parameters,{Success success, Fail fail}){
    _requestURLGet(Method.GET,url,parameters, success:success,fail:fail);
  }




  //quest //get
  static void _requestGet<T>(
      Method method, String url, parameters,{Success success,Fail fail}){
    /// restful 请求处理
    /// /base/search/hist/:user_id        user_id=27
    /// 最终生成 url 为     /base/search/hist/27
    parameters.forEach((key, value){
      if (url.indexOf(key) != -1) {
        url = url.replaceAll(':$key', value.toString());
      }
    });

    //    //参数处理
    YBLogUtils.d("--------- parameters ---------");
    YBLogUtils.e("$url---$parameters");

    YBDioUtils.requestGet(method, url, parameters,success: (result){
      //      LogUtils.d("--------- response ---------");
      YBLogUtils.d('$result');
      //      LogUtils.print_(result);
      YBBaseModel baseModel = YBBaseModel.fromJson(result);
      if (baseModel.code == 0) {
        if (success != null) {
          success(baseModel.body);
        } else {
          YBToast.showToastInfo(baseModel.message);
          success(result);
        }
      }else {
        YBToast.showToastInfo(baseModel.message);
        success(result);
      }

    },fail:(code, msg) {
      YBToast.showToastInfo(msg);
      if (fail != null) {
        fail(code);
      }
    });
  }


  //quest //get
  static void _requestURLGet<T>(
      Method method, String url, parameters,{Success success,Fail fail}){
    /// restful 请求处理
    /// /base/search/hist/:user_id        user_id=27
    /// 最终生成 url 为     /base/search/hist/27
    parameters.forEach((key, value){
      if (url.indexOf(key) != -1) {
        url = url.replaceAll(':$key', value.toString());
      }
    });

    //    //参数处理
    YBLogUtils.d("--------- parameters ---------");
    YBLogUtils.e("$url---$parameters");
    YBDioUtils.requestGet(method, url, parameters,success: (result){
      //      LogUtils.d("--------- response ---------");
      YBLogUtils.d('$result');
      //      LogUtils.print_(result);
      success(result);
    },fail:(code, msg) {
      YBToast.showToastInfo(msg);
      if (fail != null) {
        fail(code);
      }
    });
  }


  //Post 请求
  static void POSTRequest<T>(String url, parameters,{Success success, Fail fail}){
    _requestPost(Method.GET,url,parameters, success:success,fail:fail);
  }


  static void _requestPost<T>(
      Method method, String url, parameters,{Success success,Fail fail}){
    /// restful 请求处理
    /// /base/search/hist/:user_id        user_id=27
    /// 最终生成 url 为     /base/search/hist/27
    parameters.forEach((key, value){
      if (url.indexOf(key) != -1) {
        url = url.replaceAll(':$key', value.toString());
      }
    });

    //    //参数处理
    YBLogUtils.d("--------- parameters ---------");
    YBLogUtils.e("$parameters");
    YBDioUtils.requestPost(method, url, parameters,success: (result){
      //      LogUtils.d("--------- response ---------");
      YBLogUtils.d('$result');
      //      LogUtils.print_(result);
      YBBaseModel baseModel = YBBaseModel.fromJson(result);
      if (baseModel.code == 0) {
        if (success != null) {
          success(baseModel.body);
        } else {
          success(baseModel.body);
          YBToast.showToastInfo(baseModel.message);
        }
      }else {
        success(baseModel.body);
        YBToast.showToastInfo(baseModel.message);
      }

    },fail:(code, msg) {
      YBToast.showToastInfo(msg);
      if (fail != null) {
        fail(code);
      }
    });
  }


  //POST 上传图片
  static void uploadImageInfo<T>(String url,paramsters,{Success success,Fail fail}){
    _uploadImagePost(url, paramsters,success: success,fail: fail);
  }


  //上传图片
  static void _uploadImagePost<T>(String url, parameters,{Success success,Fail fail}){

    YBDioUtils.requestuploadImage(url, parameters,success: (result){
      //      LogUtils.d("--------- response ---------");
      YBLogUtils.d('$result');
      //      LogUtils.print_(result);
      YBBaseModel baseModel = YBBaseModel.fromJson(result);
      if (baseModel.code == 0) {
        if (success != null) {
          success(baseModel.body);
        } else {
          success(baseModel.body);
          YBToast.showToastInfo(baseModel.message);
        }
      }else {
        success(baseModel.body);
        YBToast.showToastInfo(baseModel.message);
      }

    },fail:(code, msg) {
      YBToast.showToastInfo(msg);
      if (fail != null) {
        fail(code);
      }
    });
  }

}